package com.qianji.petmall.order.controller;

import com.qianji.common.utils.R;
import com.qianji.petmall.order.entity.OrderEntity;
import com.qianji.petmall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author liang zai
 * @date 2022/4/21 17:18
 * @description
 */
@Slf4j
@RestController
public class RabbitController {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMassage")
    public R sendMassage(@RequestParam(value = "num",defaultValue = "10") Integer num){
        //如果发送的消息是个对象，我们会使用序列化机制，我们可以实现Serializable接口
        for (int i=0;i<num;i++) {
            if (i%2==0){
                OrderReturnReasonEntity orderReturnReason = new OrderReturnReasonEntity();
                orderReturnReason.setName("不要了" + i);
                orderReturnReason.setId(1L);
                orderReturnReason.setCreateTime(new Date());
                rabbitTemplate.convertAndSend("hello.java.exchange", "hello.java", orderReturnReason, new CorrelationData(UUID.randomUUID().toString()));
                //发送的对象的消息可以是一个JSON
//                log.info("消息发送完成{}", orderReturnReason);
            }else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello.java.exchange", "hello2.java", orderEntity,
                        new CorrelationData(UUID.randomUUID().toString()));
            }

        }
        return R.ok();
    }

}
